#!/usr/bin/perl -w 
# _NSPTOGNUPLOT_
# POST PROCESSING SCRIPT TO CONVERT
# NSP OUTPUT TO A SERIES OF GNUPLOT
# FILES, ONE FILE PER TIMESTEP
#-----------------------------------

use Getopt::Long;
use POSIX;

my $nspfile = '';
my $line = '';
my $x = '';
my $y = '';
my $z = '';
my $p = '';
my @breakout = '';

#-----------------------------------------------LINE_PARSE
sub line_parse {
    my (@lbreak) = @_[0];
    my ($lx)     = $_[1];
    my ($ly)     = $_[2];
    my ($lz)     = $_[3];
    my ($lp)     = $_[4];

    
}

#-----------------------------------------------USAGE
sub usage(){
    printf( "%s\n", "::NSPTOGNUPLOT::" );
    printf( "%s\n", "USAGE : nsptognuplot <OPTIONS>" );
    printf( "%s\n", "------------------------------------------" );
    printf( "%s\n", "<OPTIONS>" );
    printf( "%s\n", "--nsp /path/to/output.nsp :: NSP FILE" );
    printf( "%s\n", "--x :: PROCESS X VELOCITY DIMENSION" );
    printf( "%s\n", "--y :: PROCESS Y VELOCITY DIMENSION" );
    printf( "%s\n", "--z :: PROCESS Z VELOCITY DIMENSION" );
    printf( "%s\n", "--p :: PROCESS PRESSURE GRADIENT" );
    printf( "%s\n", "------------------------------------------" );
}


#-- PARSE COMMAND LINE ARGS
$result = GetOptions( 'nsp=s' => \$nspfile, 
		      'x' => \$x, 
		      'y' => \$y, 
		      'z' => \$z, 
		      'p' => \$p);

if( $result != 1 ) {
    usage();
    exit -1;
}

#-- SANITY CHECK 
if( length($nspfile) <=0 ) {
    usage();
    exit -1;
}

if( !$x && !$y && !$z && !$p ) {
    usage();
    exit -1;
}

#-- OPEN THE FILE 
open( INFILE, "<$nspfile" ) || die "NSPTOGNUPLOT: COULD NOT OPEN NSP FILE: $nspfile";
undef $/;

#-- PARSE THE FILE 
while (<INFILE>)  {
    chomp;

    $line = $_;
    if( $line =~ /#*/ ){
	#-- FOUND A COMMENT
    }else {
	@breakout = split(/\t/, $line);
	line_parse( @breakout, $x, $y, $z, $p );
    }
}

#-- CLOSE THE FILE 
close( INFILE );

exit 0;
